<template>
  <div class="container-box">
    <el-row :gutter="20" class="header">
      <el-col :span="7">
        <el-input placeholder="请输入样木名称..." v-model="queryForm.query" clearable></el-input>
      </el-col>
      <el-button type="primary" :icon="Search" @click="initTreeList">搜索</el-button>
      <el-button type="success" :icon="DocumentAdd" @click="handleDialogValue(null)">新增</el-button>
      <el-popconfirm title="您确定批量删除这些记录吗？" @confirm="handleDelete(null)">
        <template #reference>
          <el-button type="danger" :disabled="delBtnStatus" :icon="Delete">批量删除</el-button>
        </template>
      </el-popconfirm>
    </el-row>

    <el-table v-loading="loading" :data="tableData" stripe border style="width: 100%"
      @selection-change="handleSelectionChange">
      <el-table-column type="selection" />
      <el-table-column prop="tree" label="样木ID" fixed align="center" show-overflow-tooltip />
      <el-table-column prop="weight" label="总干重" align="center" show-overflow-tooltip />
      <el-table-column prop="ground" label="地上干重" align="center" show-overflow-tooltip />
      <el-table-column prop="root_ratio" label="根茎比" align="center" show-overflow-tooltip />

      <el-table-column label="干干重" align="center">
        <el-table-column prop="trunk" label="小计" align="center" show-overflow-tooltip />
        <el-table-column prop="trunkT" label="上部" align="center" show-overflow-tooltip />
        <el-table-column prop="trunkC" label="中部" align="center" show-overflow-tooltip />
        <el-table-column prop="trunkB" label="下部" align="center" show-overflow-tooltip />
      </el-table-column>

      <el-table-column label="皮干重" align="center">
        <el-table-column prop="bark" label="小计" align="center" show-overflow-tooltip />
        <el-table-column prop="barkT" label="上部" align="center" show-overflow-tooltip />
        <el-table-column prop="barkC" label="中部" align="center" show-overflow-tooltip />
        <el-table-column prop="barkB" label="下部" align="center" show-overflow-tooltip />
      </el-table-column>

      <el-table-column label="枝干重" align="center">
        <el-table-column prop="branch" label="小计" align="center" show-overflow-tooltip />
        <el-table-column prop="branchT" label="上部" align="center" show-overflow-tooltip />
        <el-table-column prop="branchC" label="中部" align="center" show-overflow-tooltip />
        <el-table-column prop="branchB" label="下部" align="center" show-overflow-tooltip />
      </el-table-column>

      <el-table-column label="叶干重" align="center">
        <el-table-column prop="leaf" label="小计" align="center" show-overflow-tooltip />
        <el-table-column prop="leafT" label="上部" align="center" show-overflow-tooltip />
        <el-table-column prop="leafC" label="中部" align="center" show-overflow-tooltip />
        <el-table-column prop="leafB" label="下部" align="center" show-overflow-tooltip />
      </el-table-column>

      <el-table-column label="根干重" align="center">
        <el-table-column prop="root" label="小计" align="center" show-overflow-tooltip />
        <el-table-column prop="rhizome" label="根茎" align="center" show-overflow-tooltip />
        <el-table-column prop="root_large" label="粗根" align="center" show-overflow-tooltip />
        <el-table-column prop="root_fine" label="细根" align="center" show-overflow-tooltip />
      </el-table-column>

      <el-table-column prop="remark" label="备注" align="center" show-overflow-tooltip />
      <el-table-column prop="action" label="操作" fixed="right" align="center" width="100px">
        <template v-slot="scope">
          <el-button-group class="ml-3" size="small">
            <el-button v-if="scope.row.code != 'superAdmin'" type="success" :icon="Edit"
              @click="handleDialogValue(scope.row.tree)" />
            <el-popconfirm v-if="scope.row.code != 'superAdmin'" title="您确定要删除这条记录吗？"
              @confirm="handleDelete(scope.row.id)">
              <template #reference>
                <el-button type="danger" :icon="Delete" />
              </template>
            </el-popconfirm>
          </el-button-group>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination v-model:currentPage="queryForm.pageNum" v-model:page-size="queryForm.pageSize" size="small"
      :page-sizes="[15, 30, 50, total > 100 ? total : 100]" layout="total, sizes, prev, pager, next" :total="total"
      @size-change="handleSizeChange" @current-change="handleCurrentChange" />

    <el-collapse v-model="activeNames" expand-icon-position="left">
      <el-collapse-item title="未录入干重的样木" name="1" :icon="CaretRight">
        <el-table v-loading="loading2" :data="NoDWTree" style="height: 460px;">
          <el-table-column prop="id" label="样木ID" sortable align="center" show-overflow-tooltip />
          <el-table-column prop="address_city" label="市" sortable align="center" show-overflow-tooltip />
          <el-table-column prop="address_name" label="样地名称" sortable align="center" show-overflow-tooltip />
          <el-table-column prop="treeSpecies_name" label="树种" align="center" show-overflow-tooltip />
          <el-table-column prop="number" label="编号" align="center" show-overflow-tooltip />

          <el-table-column prop="remark" label="备注" align="center" show-overflow-tooltip />
          <el-table-column prop="action" label="操作" fixed="right" align="center" width="120px">
            <template v-slot="scope">
              <el-button-group class="ml-3" size="small">
                <el-button v-if="scope.row.code != 'superAdmin'" type="success" :icon="Edit"
                  @click="handleDialogValue(scope.row.id)">录入数据</el-button>
              </el-button-group>
            </template>
          </el-table-column>
        </el-table>

        <el-pagination v-model:currentPage="queryForm2.pageNum" v-model:page-size="queryForm2.pageSize" size="small"
          :page-sizes="[10, 30, 50, total2 > 100 ? total2 : 100]" layout="total, sizes, prev, pager, next"
          :total="total2" @size-change="handleSizeChange2" @current-change="handleCurrentChange2" />
      </el-collapse-item>
    </el-collapse>

    <Dialog v-model="dialogVisible" :dialogVisible="dialogVisible" :id="id" :dialogTitle="dialogTitle"
      @initTreeList="initTreeList"></Dialog>
  </div>
</template>

<script setup>
import { Search, Delete, DocumentAdd, Edit, Tools, RefreshRight } from '@element-plus/icons-vue'
import { onMounted, ref } from 'vue'
import Dialog from './components/dialog.vue'
import api, { getServerUrl } from "@/util/request";
import { ElMessage, ElMessageBox } from 'element-plus'
import { CaretRight } from '@element-plus/icons-vue'

const activeNames = ref(['1'])
const loading = ref(true)
const loading2 = ref(true)
const delBtnStatus = ref(true)
const id = ref(-1)
const dialogVisible = ref(false)
const dialogTitle = ref('')
const total = ref(0)
const total2 = ref(0)
const tableData = ref([])
const multipleSelection = ref([])
const queryForm = ref({
  query: '',
  pageNum: 1,
  pageSize: 15
})
const queryForm2 = ref({
  query: '',
  pageNum: 1,
  pageSize: 10
})

const initTreeList = async () => {
  loading.value = true
  await api.post("treedw/search", queryForm.value).then((res) => {
    // tableData.value = res.data.data;
    // 将每一项的 id 转为数字
    tableData.value = (res.data.data || []).map(item => ({
      ...item,
      id: Number(item.id)
    }));

    total.value = res.data.total;
    loading.value = false
  })
}

const handleSelectionChange = (selection) => {
  console.log("勾选了:", selection)
  multipleSelection.value = selection;
  delBtnStatus.value = selection.length == 0;
}

const handleSizeChange = (pageSize) => {
  queryForm.value.pageNum = 1;
  queryForm.value.pageSize = pageSize;
  initTreeList();
}

const handleCurrentChange = (pageNum) => {
  queryForm.value.pageNum = pageNum;
  initTreeList();
}

const handleDelete = async (id) => {
  var ids = []
  if (id) {
    ids.push(id)
  } else {
    multipleSelection.value.forEach(row => {
      ids.push(row.id)
    })
  }
  api.del("treedw/action", ids).then((res) => {
    if (res.data.code == 200) {
      ElMessage({
        type: 'success',
        message: '执行成功!'
      })
      initTreeList();
    } else {
      ElMessage({
        type: 'error',
        message: res.data.msg,
      })
    }
  })
}

const handleDialogValue = (treeId) => {
  if (treeId) {
    id.value = treeId;
    dialogTitle.value = "样木信息添加/修改"
  } else {
    id.value = -1;
    dialogTitle.value = "样木信息添加/修改"
  }
  dialogVisible.value = true
}

// ================================== 未录入干重的样木 ==================================
const NoDWTree = ref([])
const initNoDWTree = async () => {
  loading2.value = true
  await api.post('treedw/getnodwtree', queryForm2.value).then((res) => {
    if (res.data.code == 200) {
      NoDWTree.value = res.data.data
      total2.value = res.data.total
      loading2.value = false
    }
  })
}
const handleSizeChange2 = (pageSize) => {
  queryForm2.value.pageNum = 1;
  queryForm2.value.pageSize = pageSize;
  initNoDWTree();
}

const handleCurrentChange2 = (pageNum) => {
  queryForm2.value.pageNum = pageNum;
  initNoDWTree();
}

onMounted(() => {
  initTreeList();
  initNoDWTree();
})

</script>

<style lang="scss" scoped>
.header {
  padding-bottom: 16px;
  box-sizing: border-box;
}

.el-pagination {
  float: left;
  padding: 10px;
  box-sizing: border-box;
}

::v-deep th.el-table__cell {
  word-break: break-word;
  background-color: #f8f8f9 !important;
  color: #515a6e;
  height: 40px;
  font-size: 13px;
}
</style>
